<!--
 * @Author: your name
 * @Date: 2020-03-12 16:29:50
 * @LastEditTime: 2020-03-29 11:52:32
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \smart-community\src\views\integral\declareRecord.vue
 -->
<template>
  <div class="declareRecord">
    <van-nav-bar title="申报记录" left-arrow @click-left="$router.go(-1)" fixed />
    <div class="integralDeclare-body">
      <van-tabs @click="changeTab">
        <van-tab title="我的申报">
          <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
            <van-list
              v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              @load="onLoad"
              :offset="50"
            >
              <div
                v-for="(item, index) in myDeclare"
                :key="index"
                class="van-cell van-panel__header"
                @click="$router.push({ name: 'integralDeclareDetails',params:{id:item.id} })"
              >
                <div class="van-cell__title">
                  <span>{{ item.content }}</span>
                  <p
                    v-if="item.status == 1 || item.status == 2 || item.status == 6"
                    class="van-cell__label"
                  >审核中</p>
                  <p v-if="item.status == 3" class="van-cell__label">被举报</p>
                  <p v-if="item.status == 4" class="van-cell__label">审核通过</p>
                  <p v-if="item.status == 5" class="van-cell__label">审核驳回</p>
                  <div class="van-cell__label">{{ item.creationDate }}</div>
                </div>
                <div class="van-cell__value van-panel__header-value">
                  <!-- <van-tag type="primary">{{item.statusName}}</van-tag> -->
                  <!-- <van-tag v-if="item.status == 1" type="warning">审核中</van-tag>
                  <van-tag v-if="item.status == 2" type="primary">公示中</van-tag>
                  <van-tag v-if="item.status == 3" color="#7232dd">被举报</van-tag>
                  <van-tag v-if="item.status == 4" type="success">审核通过</van-tag>
                  <van-tag v-if="item.status == 5" type="danger">审核驳回</van-tag>
                  <van-tag v-if="item.status == 6" type="success">审核通过</van-tag>-->
                </div>
              </div>
            </van-list>
          </van-pull-refresh>
        </van-tab>
        <van-tab title="我的审核" v-if="flag">
          <van-pull-refresh v-model="isLoadingAudit" @refresh="onRefreshAudit">
            <van-list
              v-model="loadingAudit"
              :finished="finishedAudit"
              finished-text="没有更多了"
              @load="onLoadAudit"
              :offset="50"
            >
                <div v-for="(item, index) in myAudit" :key="index"  class="van-cell van-panel__header">
                    <div class="van-cell__title_">
                      <span>{{ item.content }}</span>
                      <div class="van-cell__label">{{ item.creationDate }}</div>
                    </div>
                    <div class="van-cell__value van-panel__header-value">
                      <van-button type="info" @click.stop="integral_edit_builder(item)">审核</van-button>
                    </div>
                </div>
            </van-list>
          </van-pull-refresh>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import integral from "../../controller/integral";
import constant from "@/controller/constant.js";
export default {
  data() {
    return {
      loadingAudit: false,
      finishedAudit: true,
      isLoadingAudit: false,
      loading: false,
      finished: true,
      isLoading: false,
      myDeclare: [],
      myAudit: [],
      curPage: 1,
      pageSize: 10,
      curPageAudit: 1,
      pageSizeAudit: 10,
      limit: {},
      flag: false
    };
  },
    created() {
       this.limit = JSON.parse(localStorage.getItem("userInfo"));
        this.limit.permissions.forEach(item => {
      if (item.indexOf("社区积分$igApply$pagesApplyAuditFront") !== -1) {
        this.flag = true;
      }
    });
  },
  mounted() {
    this.integral_my_declare();
    this.integral_my_audit();
  },
  methods: {
    // 编辑审核
    integral_edit_builder(item) {
      let data = {
        applyId: item.id,
        neighborhoodId: item.neighborhoodId,
        content: item.content,
        applyTypeName: item.applyTypeName,
        applyMode: item.applyMode,
        buildingCode: item.buildingCode,
        applyName: item.applyName,
        applyIdentityName: item.applyIdentityName,
        image: item.image,
        applyIdentityId: item.applyIdentityId,
        applyTypeId: item.applyTypeId,
        applyUserId: item.applyUserId
      };
      const str = JSON.stringify(data);
      this.$router.push(`editEeclareRecord/${encodeURIComponent(str)}`);
    },

    changeTab(name, title) {
      if (name === 0 && title === "我的申报") {
        this.myDeclare = [];
        this.curPage = 1;
        this.integral_my_declare();
      }
      if (name === 1 && title === "我的审核") {
        this.myAudit = [];
        this.curPageAudit = 1;
        this.integral_my_audit();
      }
    },
    // 我的审核
    onLoadAudit() {
      this.isLoadingAudit = false;
      this.onRefreshAudit();
    },
    // 我的审核
    onRefreshAudit() {
      // this.isLoadingAudit = false;
      // if (!this.finishedAudit) {
      this.curPageAudit += 1;
      this.isLoadingAudit = false;
      this.integral_my_audit();
      // }
    },
    // 我的申报刷新
    onLoad() {
      this.isLoading = false;
      this.onRefresh();
    },
    // 我的申报刷新
    onRefresh() {
      // this.isLoading = false;
      // if (!this.finished) {
      this.curPage += 1;
      this.isLoading = false;
      this.integral_my_declare();
      // }
    },
    // 我的申报
    integral_my_declare() {
      let data = {
        curPage: this.curPage,
        pageSize: this.pageSize
      };
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner"
      });     
      integral
        .integral_my_declare(data)
        .then(res => {
          if (res.code == "0") {
            this.finished = false;
            if (
              (res.datas && res.datas.records.length === 0) ||
              res.datas === null
            ) {
              // this.$toast("暂无更多数据");
              this.loading = false;
              this.isLoading = false;
              this.finished = true;
              return;
            }
            this.$nextTick(() => {
              this.loading = false;
              this.isLoading = false;
            });
            if (res.datas.records.length < this.pageSize) {
              this.finished = true;
            }
            if (
              (res.datas && res.datas.records.length !== 0) ||
              res.datas !== null
            ) {
              this.myDeclare = [...this.myDeclare, ...res.datas.records];
              this.$toast.clear();
            }
          } else {
            if (res && res.msg) {
              this.$toast(res.msg);
            } else {
              this.$toast(constant.unusualMsg);
            }
          }
        })
        .catch(() => {
          this.$toast(constant.unusualMsg);
        });
      
    
    },
    // 我的审核
    integral_my_audit() {
      let data = {
        curPage: this.curPageAudit,
        pageSize: this.pageSizeAudit
      };
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        loadingType: "spinner"
      });
        if (this.flag === true) {
      integral
        .integral_my_audit(data)
        .then(res => {
          if (res.code == "0") {
            this.finishedAudit = false;
            if (
              (res.datas && res.datas.records.length === 0) ||
              res.datas === null
            ) {
              // this.$toast("暂无更多数据");
              this.loadingAudit = false;
              this.isLoadingAudit = false;
              this.finishedAudit = true;
              return;
            }
            this.$nextTick(() => {
              this.loadingAudit = false;
              this.isLoadingAudit = false;
            });
            if (res.datas.records.length < this.pageSizeAudit) {
              this.finishedAudit = true;
            }
            if (
              (res.datas && res.datas.records.length !== 0) ||
              res.datas !== null
            ) {
              this.myAudit = [...this.myAudit, ...res.datas.records];
              this.$toast.clear();
            }
          } else {
            if (res && res.msg) {
              this.$toast(res.msg);
            } else {
              this.$toast(constant.unusualMsg);
            }
          }
        })
        .catch(() => {
          this.$toast(constant.unusualMsg);
        });
    }
    }
  },

  beforeRouteEnter(to, from, next) {
    // debugger;
    if (from.name == "editEeclareRecord" || from.name == "integralDeclare") {
      to.meta.isBack = false;
      to.meta.keepAlive = false;
      //判断是从哪个路由过来的，
      //如果是editEeclareRecord过来的，表明当前页面需要刷新获取新数据，
    }
    next();
  },
  activated() {
    if (!this.$route.meta.isBack) {
      // 如果isBack是false，表明需要获取新数据，否则就不再请求，直接使用缓存的数据
      // 如果isFirstEnter是true，表明是第一次进入此页面或用户刷新了页面，需获取新数据
      // this.myAudit= [];// 把数据清空，可以稍微避免让用户看到之前缓存的数据
      this.myDeclare = [];
      this.curPage = 1;
      this.pageSize = 10;
      this.integral_my_declare();
      this.myAudit = [];
      this.curPageAudit = 1;
      this.pageSizeAudit = 10;
      this.integral_my_audit();
    }
    // 恢复成默认的false，避免isBack一直是true，导致下次无法获取数据
    this.$route.meta.isBack = false;
    // 恢复成默认的false，避免isBack一直是true，导致每次都获取新数据
    // this.isFirstEnter = false;
  }
};
</script>

<style lang="scss" scoped>
.declareRecord {
  background: #fff;
  .van-tab__pane {
    padding: 20px 10px;
  }
  .van-tag {
    width: 60px;
    text-align: center;
    padding: 5px;
    margin: 5px 5px 0 0;
  }
  .van-cell__title_ {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /deep/ .van-cell__title {
    flex: inherit;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    span {
      font-size: 16px;
    }
  }
  /deep/ .van-cell__label {
    font-size: 14px;
  }
  /deep/ .van-button--normal {
    height: 30px;
    line-height: 30px;
  }
}
</style>
